package com.thd.springboottest.camunda.controller;

import com.thd.springboottest.camunda.service.WorkFlowService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * com.thd.springboottest.camunda.controller.CamundaController
 *
 * @author: wanglei62
 * @DATE: 2022/5/26 17:01
 **/
@Controller
@RequestMapping("/camunda/addParalelSubProcess")
public class Camunda08addParalelSubProcess {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private WorkFlowService workFlowService;
    /**
     * 部署
     */
    // url : http://127.0.0.1:8899/thd/camunda/addParalelSubProcess/deploy
    @RequestMapping(value="/deploy",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deploy(){
        String deployId = this.workFlowService.deploy("processes/08addParallelSubProcess.bpmn");
        System.out.println("deployId:" + deployId);
        return ResponseEntity.ok("SUCCESS");
    }

    /**
     * 开启流程
     */
    // url : http://127.0.0.1:8899/thd/camunda/addParalelSubProcess/startProcess/[businessKey:gateway001]
    @RequestMapping(value="/startProcess/{businessKey}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> startProcess(@PathVariable String businessKey){
        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("addParalelSubProcess",businessKey);
        System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }

    /*
    设置流程变量
    http://127.0.0.1:8899/thd/camunda/addVariableForProcessInstanceByBusinessKey/[businessKey]

    {
        "users":[
            {
                "applier":"zhangsan1",
                "approver":"lisi1"
            },
            {
                "applier":"zhangsan2",
                "approver":"lisi2"
            }
        ]
    }
     */



    /*
    完成代办http://127.0.0.1:8899/thd/camunda/completeTask/f462fb36-dffd-11ec-908d-e86a64fc0114
     */

    /*
    查询待办  http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/a01
    待办为2个subTaskA , 待办人是流程变量users中的 zhangsan1,zhangsan2
     */

    /**
     * 加签
     */
    // url : http://127.0.0.1:8899/thd/camunda/addParalelSubProcess/addTask/[businessKey]
    @RequestMapping(value="/addTask/{businessKey}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> addTask(@PathVariable String businessKey){
        Map processVariable = new HashMap();
        processVariable.put("applier","wangwu");
        processVariable.put("approver","zhaoliu");
        ProcessInstance pi = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceBusinessKey(businessKey).singleResult();
                processEngine.getRuntimeService().createProcessInstanceModification(pi.getId())
                        .startBeforeActivity("subProcess") // 会签节点的activityID (如果是子流程则选择子流程这个大节点的ID)
                        .setVariableLocal("user",processVariable) // 设置流程变量
                        .execute();

        System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }

    /*
    查询待办  http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/a01
    待办为3个subTaskA , 多出来的是加签加出来的代办人是wangwu的代办
     */


    /**
     * 减签
     * 注：减签的时候传的activityInstanceId是通过task找到execution,再通过execution找到其act_inst_id  用act_inst_id的值
     */
    // url : http://127.0.0.1:8899/thd/camunda/addParalelSubProcess/subtractTask/[businessKey]/activityInstanceId
    @RequestMapping(value="/subtractTask/{businessKey}/{activityInstanceId}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> subTractTsk(@PathVariable String businessKey,@PathVariable String activityInstanceId){
        ProcessInstance pi = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceBusinessKey(businessKey).singleResult();
        processEngine.getRuntimeService().createProcessInstanceModification(pi.getId())
                .cancelActivityInstance(activityInstanceId) // execution表中act_inst_id_字段的值
                .execute();

        System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }


    /*
    查询待办  http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/a01
    待办为4个TaskB , 因为减签删掉了一个
     */

    /*
    完成zhangsan,lisi,wangwu,zhaoliu的代办，

    最后剩下sunqi的代办
     */

    /*
    完成sunqi的代办
     */

    /*
    查询待办
    TASKC,流程已走下去了。
     */

    /*
    完成代办C
     */

    /*
    查询待办
    TASKB - zhangsan   串行任务

    /*
    执行加签
     */

    /**
     * 加签
     */
    // url : http://127.0.0.1:8899/thd/camunda/addAndsubtractParallelTask/addTaskForSequence/[businessKey]
    @RequestMapping(value="/addTaskForSequence/{businessKey}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> addTaskForSequence(@PathVariable String businessKey){
        ProcessInstance pi = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceBusinessKey(businessKey).singleResult();
        processEngine.getRuntimeService().createProcessInstanceModification(pi.getId())
                .startBeforeActivity("TASKD") // 会签节点的activityID
                .setVariableLocal("user","sunqi") // 设置流程变量
                .execute();

        System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }
}
